using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GroundControl : MonoBehaviour
{
    // 背景速度
    public float speed = 2f;
    // 随机地面数组
    public GameObject[] groundPrefabs;

    void Update()
    {
        if(PlayerControl.hp == 0){
            return;
        }
        // 遍历地面，子物体
        foreach(Transform tran in transform){
            Vector3 pos = tran.position;
            pos.x -= speed * Time.deltaTime;
            if(pos.x < -7.2f){
                Transform newTrans = Instantiate(groundPrefabs[Random.Range(0,groundPrefabs.Length)],transform).transform;
                Vector2 newPos = newTrans.position;
                newPos.x = pos.x+7.2f*2;
                newTrans.position = newPos;
                Destroy(tran.gameObject);
            }
            tran.position = pos;
        }
    }
}
